<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue">
		</om-autoform>
	</om-addpage>
</template>

<script>
	import {
		buildFormData,
		changeEditor,
		configSelectData,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				busCode: "",
				InstCacheKey: 'hr-js-workLiaison-inst-cache',
				userCackeKey: 'hr-js-workLiaison-userName-cache',
				fileKey: 44
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userId', 'orgId'])
		},
		methods: {
			getPosComboboxByUser(id){
				let _that = this
				_that.om.request(
					"workLiaison/getPosComboboxByUser", {
						"userId":id
					}).then(i => {
					_that.$refs.autoform.changeEditor("productInfo", "postId", "data", configSelectData(i, "id", "posname"))
					if(_that.$refs.autoform.hasEditor("productInfo", "postId")){
						if(!_that.$refs.autoform.formdata.productInfo){
							_that.$refs.autoform.initSelectVal("productInfo", "postId")
						}else{
							_that.$refs.autoform.initSelectIndex("productInfo", "postId")
						}
					}
				})
			},
			onSelectEvent(e) {
				let _that = this
				_that.om.request(
					"workLiaison/getUser",
					{
						eventId:e.id, 
						orgId: _that.orgId
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("productInfo", "userId",'isSearchUser',true)
					_that.$refs.autoform.changeEditor("productInfo", "userId", {
						link: "../../public/searchUser/searchUser",
						"linkParam": {
							hasDefaultData:true,
							changefield:'userId',
							namefield:'userName',
							label2:'userName',
							isSingle:true,
							cachename: _that.userCackeKey
						},
					}, null)
					uni.setStorage({
						key:"searchUserDefaultList",
						data:res
					})
				})
				this.clearUser()
			},
			onSelectPost(e) {
				this.$refs.autoform.formdata.mechanism = e.instName
			},
			clearUser() {
				let _that = this
				_that.$refs.autoform.clear("productInfo","postId")
				_that.$refs.autoform.clear("productInfo","userId")
				_that.$refs.autoform.clear("productInfo","mechanism")
			},
			clearEvent() {
				this.$refs.autoform.formdata.mechanism = ''
				this.$refs.autoform.formdata.eventCategoryId = ''
				this.$refs.autoform.formdata.eventCategory = ''
				this.$refs.autoform.formdata.postId = ''
				this.$refs.autoform.formdata.userId = ''
				this.$refs.autoform.formdata.userName = ''
				this.$refs.autoform.formdata.postName = ''
				this.$nextTick(function() {
					this.$refs.autoform.initSelectIndex('productInfo', 'postId')
					this.$refs.autoform.initSelectIndex('productInfo', 'userId')
					this.$refs.autoform.initSelectIndex('productInfo', 'eventCategoryId')
				})
			},
			clearPost() {
				this.$refs.autoform.formdata.mechanism = ''
				this.$refs.autoform.formdata.postId = ''
				this.$refs.autoform.formdata.postName = ''
				this.$nextTick(function() {
					this.$refs.autoform.initSelectIndex('productInfo', 'postId')
				})
			},
			formSubmit() {
				let _that = this
				_that.formdata.updateBottomFileFlag= true
				if(Object.prototype.toString.call(_that.$refs.autoform.formdata.bottomFileList) == "[object String]"){
					_that.formdata.bottomFileListStr = _that.$refs.autoform.formdata.bottomFileList|| ''
				}else{
					_that.formdata.bottomFileListStr = JSON.stringify(_that.$refs.autoform.formdata.bottomFileList || []) || ''
				}
				delete _that.formdata.bottomFileUpload
				delete _that.formdata.bottomFileList
				if (_that.edit) {
					_that.om.formSave("workLiaison/doUpdate", _that.formdata, _that)
				} else {
					_that.om.formSave("workLiaison/doAdd", _that.formdata, _that)
				}
			},
			intLoad(options) {
				let _that = this
				let ajaxArr = [
					urlUtil.zposUrl(options.code), {
						url: "tsysdict/getCombobox.do",
						param: {
							code: '38965'
						},
						method: "get"
					}, {
						url: "workLiaison/getEventCategory",
						param: {
							orgId: this.orgId
						},
						method: "get"
					}
				]
				let fileId = options.fileId
				if (fileId) {
					ajaxArr.push({
						url: "workLiaison/getValue",
						param: {
							fileId: fileId
						}
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					_that.mainData = mainData
					changeEditor("topinfo", "zposId", "data", configSelectData(res[0], "id", "posname"),
						mainData)
					changeEditor("productInfo", "attributeCategoryId", "data", configSelectData(res[1], "code",
						"dicname"), mainData)
					changeEditor("productInfo", "eventCategoryId", "data", configSelectData(res[2], "id",
						"itemName"), mainData)
					changeEditor("productInfo", "userId",'isSearchUser',true,mainData)
					changeEditor("productInfo", "userId", {
						link: "../../public/searchUser/searchUser",
						"linkParam": {
							isSingle:true,
							cachename: _that.userCackeKey
						},
					}, null,mainData)
					let tempFileList = []
					let fileUrlList = []
					changeEditor('bottominfo','bottomFileUpload','fileUploadKey',"44",mainData)
					changeEditor("bottominfo","bottomFileUpload","tempFileList", tempFileList,mainData)
					changeEditor("bottominfo","bottomFileUpload","fileUrlList", fileUrlList,mainData)
					if (fileId) {
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for (let index in res[3].value) {
							if (fieldArr.includes(index)) {
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata, index, res[3].value[index])
								_that.$set(_that.formdata, index, res[3].value[index])
							}
						}
						if(_that.formdata.bottomFileList){
							let tempFileList = []
							let fileUrlList = _that.formdata.bottomFileList.map(t=>{
								tempFileList.push(t.fileSaveName)
								return t.fileSaveName
							})
							changeEditor("bottominfo","bottomFileUpload","tempFileList", tempFileList,mainData)
							changeEditor("bottominfo","bottomFileUpload","fileUrlList", fileUrlList,mainData)
						}
						_that.$nextTick(function() {
							_that.$refs.autoform.initSelectIndex('productInfo', 'attributeCategoryId')
							_that.$refs.autoform.initSelectIndex('productInfo', 'eventCategoryId')
							_that.$refs.autoform.initSelectIndex('topinfo', 'zposId')
							if(_that.$refs.autoform.formdata.userId){
								_that.getPosComboboxByUser(_that.$refs.autoform.formdata.userId)
							}
							if(_that.$refs.autoform.formdata.eventCategoryId){
								_that.om.request(
									"workLiaison/getUser",
									{
										eventId:_that.$refs.autoform.formdata.eventCategoryId, 
										orgId: _that.orgId
									}
								).then(res=>{
									_that.$refs.autoform.changeEditor("productInfo", "userId",'isSearchUser',true)
									_that.$refs.autoform.changeEditor("productInfo", "userId", {
										link: "../../public/searchUser/searchUser",
										"linkParam": {
											hasDefaultData:true,
											changefield:'userId',
											namefield:'userName',
											label2:'userName',
											isSingle:true,
											cachename: _that.userCackeKey
										},
									}, null)
									uni.setStorage({
										key:"searchUserDefaultList",
										data:res
									})
								})
							}
						})
					}
					_that.$nextTick(function(){
						if (!_that.$refs.autoform.formdata.zposId) {
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}
					})
				}, options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.intLoad(options)
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
					key: _that.userCackeKey,
					success: res => {
						let d  = res.data[0]
						_that.$refs.autoform.formdata.userId = d.id
						_that.$refs.autoform.formdata.userName = d.username
						_that.getPosComboboxByUser(d.id)
						uni.removeStorage({
							key: _that.userCackeKey
						})
					}
				})

			}
		}
	}
</script>

<style>
</style>
